package com.lms.service.lab;

import com.lms.dao.BaseDao;
import com.lms.dao.lab.LabDao;
import com.lms.dao.lab.LabDaoImpl;
import com.lms.bean.LabData;
import com.lms.bean.LabWeek;
import com.lms.pojo.LabIntervalData;
import com.lms.pojo.LabWeekData;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LabServiceImpl implements LabService{
    private LabDao labDao;
    private Connection connection;

    public LabServiceImpl() {
        this.labDao = new LabDaoImpl();
    }

    @Override
    public List<LabData> getLabList() {
        List<LabData> labDataList =null;

        try {
            connection=BaseDao.getConnection();
            labDataList =labDao.getLabList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return labDataList;
    }

    @Override
    public List<LabWeekData> getLabWeekDataByLabName(String labCode,String week) {
        LabWeek labWeek=null;
        List<LabWeekData> labWeekDataList=new ArrayList<>();
        /*分离初始化*/
        if ("null".equals(labCode)|| "null".equals(week) || labCode ==null || week==null){
            return labWeekDataList;
        }
        /*获取LabWeek*/
        try {
            connection=BaseDao.getConnection();
            labWeek=labDao.getLabWeekByLabName(connection,labCode,"lab_"+week);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        /*设置LabWeekData*/
        for (int i = 1; i <= 5; i++) {
            LabWeekData labWeekData = new LabWeekData();
            switch (i){
                case 1:
                    labWeekData.setInterval("1-2");
                    labWeekData.setCapacity(this.getLabTotalAndCapacity(labCode,week,"one")[1]);
                    break;
                case 2:labWeekData.setInterval("3-4");
                    labWeekData.setCapacity(this.getLabTotalAndCapacity(labCode,week,"two")[1])
                    ;break;
                case 3:labWeekData.setInterval("5-6");
                    labWeekData.setCapacity(this.getLabTotalAndCapacity(labCode,week,"three")[1]);
                    break;
                case 4:labWeekData.setInterval("7-8");
                    labWeekData.setCapacity(this.getLabTotalAndCapacity(labCode,week,"four")[1]);
                    break;
                case 5:labWeekData.setInterval("9-10");
                    labWeekData.setCapacity(this.getLabTotalAndCapacity(labCode,week,"five")[1]);
                    break;
            }
            if (labWeek!=null){
                switch (labWeek.get(i)){
                    case 1:labWeekData.setOpen("是");break;
                    case 0:labWeekData.setOpen("否");break;
                }
                labWeekDataList.add(labWeekData);
            }
        }

        return labWeekDataList;
    }

    @Override
    public int[] getLabTotalAndCapacity(String labCode,String week,String interval) {
        int[] totalAndCapacity=new int[2];

        try {
            connection = BaseDao.getConnection();
            totalAndCapacity[0]=labDao.getLabTotal(connection,labCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        /*总量-时间段内申请人数=余量*/
        totalAndCapacity[1]=totalAndCapacity[0]-this.getApplyCount(labCode,week,interval);
        return totalAndCapacity;
    }

    @Override
    public int getApplyCount(String labCode, String week, String interval) {
        try {
            connection=BaseDao.getConnection();
            return labDao.getApplyCount(connection,labCode,week,interval);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public LabData getLabDataByLabCode(String labCode) {
        try {
            connection=BaseDao.getConnection();
            return labDao.getLabDataByLabCode(connection,labCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<LabIntervalData> getLabIntervalData(String week, String interval) {
        List<LabIntervalData> labIntervalDataList=new ArrayList<>();
        /*分离初始化*/
        if ("null".equals(week)|| "null".equals(interval)){
            return labIntervalDataList;
        }

        try{
            connection=BaseDao.getConnection();
            return labDao.getLabIntervalData(connection,week,interval);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return labIntervalDataList;
    }

    @Override
    public String getLabCodeByLabName(String labName) {
        connection=BaseDao.getConnection();
        try {
            return labDao.getLabCodeByLabName(connection,labName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public LabData getLabDataByLabName(String labName) {
        connection=BaseDao.getConnection();
        try {
            return labDao.getLabDataByLabName(connection,labName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int labModify(String labName, String labTotal, String labTag, String labIntroduce) {
        connection=BaseDao.getConnection();
        try {
            return labDao.labModify(connection,labName,labTotal,labTag,labIntroduce);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
